package com.zk.controller;

import com.zk.model.*;
import com.zk.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class UserController {
    @Resource
    private UserService userService;
    //查看购物车
    @GetMapping("/wishlist")
    public String lookCart(HttpSession session,Model model){
        User user= (User) session.getAttribute("user");
        int userId=user.getId();
        List<Cart> cartList=userService.selectCarts(userId);
        model.addAttribute("goodList",cartList);
        return "wishlist";
    }
//    添加购物车
    @GetMapping("/addCart")
    public String addCart( String num, HttpSession session,Model model){
        User user= (User) session.getAttribute("user");
        Goods goods= (Goods) session.getAttribute("id");
        model.addAttribute("id",goods);
        int goodId=goods.getId();
        int userId=user.getId();
        int i=userService.addCart(userId,goodId,Integer.valueOf(num));
        List<Cart> cartList=userService.selectCarts(userId);
        int nums=cartList.size();
        session.setAttribute("cartNum",nums);
        return "shangping";
    }
//    商品详情
    @GetMapping("/shangping")
    public String buy(Model model,Integer id,HttpSession session){
        Goods goods=userService.queryGoods(id);
        model.addAttribute("id",goods);
        session.setAttribute("id",goods);
        return "shangping";
    }
//    首页
    @GetMapping("/index")
    public String backHome(){
        return "index";
    }
//    登录
    @PostMapping("/userselect")
    public String selectUser(String name, String password, HttpSession session){
        User user=userService.queryUser(name,password);
        if (user==null){
            return "longin";
        }
        int userId=user.getId();
        List<Cart> cartList=userService.selectCarts(userId);
        int num=cartList.size();
        session.setAttribute("cartNum",num);
        session.setAttribute("user",user);
        return "index";
    }
//    注册
    @PostMapping("/regiest")
    @ResponseBody
    public String regiestUser(String name,String password,String adress,String phone,String age){
        int num=userService.addUser(name,password,adress,phone,age);
        return num==1?"注册成功":"注册失败";
    }
    //通过购物车下单
    @GetMapping("/orderCart")
    public String addOrder(Model model,String num,String goodId,HttpSession session){
        User user= (User) session.getAttribute("user");
        userService.deleteGoods(user.getId(),Integer.valueOf(goodId));
        Goods goods=userService.queryGoods(Integer.valueOf(goodId));
        List<Cart> cartList=userService.selectCarts(user.getId());
        int nums=cartList.size();
        session.setAttribute("cartNum",nums);
        session.setAttribute("id",goods);
        Integer endPrice=goods.getPrice()*Integer.valueOf(num);
        model.addAttribute("endPrice",endPrice);
        model.addAttribute("orderNum",num);
        return "order";
    }
    //下单
    @GetMapping("/order")
    public String addOrder(Model model,HttpSession session,String num){
        Goods goods= (Goods) session.getAttribute("id");
        Integer endPrice=goods.getPrice()*Integer.valueOf(num);
        session.setAttribute("endPrice",endPrice);
        session.setAttribute("orderNum",num);
        return "order";
    }
    //确认订单信息
    @GetMapping("/orderNext")
    public String queryOrderNext(String endPrice,Model model,String orderNum){
        SimpleDateFormat simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String str = simpleDateFormat.format(date);
        Random random = new Random();
        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
        String orderId=rannum+str;
        model.addAttribute("orderNum",orderNum);
        model.addAttribute("endPrice",endPrice);
        model.addAttribute("orderId",orderId);
        return "orderNext";
    }
    //订单创建 支付之后
    @GetMapping("/addOrder")
    public String addOrders(HttpSession session,String orderNum,String endPrice,String orderId){
        User user= (User) session.getAttribute("user");
        Goods goods= (Goods) session.getAttribute("id");
        int num=userService.addOrder(goods.getId(),user.getId(),orderId,Integer.valueOf(orderNum),Integer.valueOf(endPrice));
        return num>0?"succes":"nosucces";
    }
//    跳转支付成功页面
    @GetMapping("/succes")
    public String succes(){
        return "succes";
    }
//    查看订单
    @GetMapping("/myOrder")
    public String selectOrders(HttpSession session,Model model){
        User user= (User) session.getAttribute("user");
        List<Order> orderList=userService.selectOrders(user.getId());
        model.addAttribute("orderList",orderList);
        return "myOrder";
    }
    //查询商品
  @GetMapping("/selectGoodByName")
    public String selectGoodByName(String name,Model model){
        List<Goods> goodsList=userService.SelectGoodByName(name);
        model.addAttribute("goodList",goodsList);
        return "like";
}
//跳转到修改地址
    @GetMapping("/adress")
    public String adress(){
        return "adress";
    }
    //修改收货地址
    @PostMapping("/updateAdress")
    public String updateAdress(HttpSession session,String adress, String phone){
        User user= (User) session.getAttribute("user");
        Integer userId=user.getId();
        int num=userService.updateAdress(adress,phone,userId);
        user=userService.rennewUser(userId);
        session.setAttribute("user",user);
        return num==1?"order":"adress";
    }
    //跳转评价
    @GetMapping("/pj")
    public String goPj(String id,HttpSession session){
        session.setAttribute("goodsId",Integer.valueOf(id));
        return "appraise";
    }
    //添加评价
    @PostMapping("/addPj")
    public String addPj(String pj,HttpSession session){
        User user= (User) session.getAttribute("user");
        Integer goodsId= (Integer) session.getAttribute("goodsId");
        int num=userService.addPj(pj,user.getId(),goodsId);
        return "index";
    }
//    查看评价
    @GetMapping("/goPj")
    public String goPj(Model model){
        List<Pj> pjList=userService.selectPj();
        model.addAttribute("pjList",pjList);
        return "pj";
    }
    //    查看我的评价
    @GetMapping("/myPj")
    public String myPj(Model model,HttpSession session){
        User user= (User) session.getAttribute("user");
        List<Pj> pjList=userService.myPj(user.getId());
        model.addAttribute("pjList",pjList);
        return "pj";
    }
}
